package com.atguigu.springbootdemo.controller;

import com.atguigu.springbootdemo.bean.Employee;
import com.atguigu.springbootdemo.service.EmployeeService;
import com.atguigu.springbootdemo.service.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Smexy on 2022/12/5
 */
@RestController
public class EmployeeController
{
    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/emp")
    public Object handle1(String op,String lastname,Integer id,String gender,String email){

        employeeService.haha();
        //封装参数为数据模型
        Employee employee = new Employee(id, lastname, gender, email);

        switch (op){
            case "select" : if (id == null){
                return "id非法!";
            }else {
                Employee e = employeeService.getById(id);
                return  e == null ? "查无此人" : e;
            }

            case "update": if (id == null){
                return "id非法!";
            }else {
                employeeService.saveOrUpdate(employee);
                return "更新成功!";
            }

            case "insert": {
                employeeService.save(employee);
                return "插入成功!";
            }

            case "delete" : if (id == null){
                return "id非法!";
            }else {
                //调用service层  推荐
                //employeeService.removeById(id);
                //调用Dao层
                employeeService.getBaseMapper().deleteById(id);
                return  "删除成功!";
            }

            default: return "成功";

        }

    }

    @RequestMapping(value = "/getAllEmp")
    public Object handle2(){

       return employeeService.list();

    }

}
